﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ProjetLabo.Class.Utils;

namespace ProjetLabo
{
    /// <summary>
    /// Logique d'interaction pour Step2Window.xaml
    /// </summary>
    public partial class Step2Window : Window
    {
        public Step2Window()
        {
            InitializeComponent();

            this.HideImportControls();

            this.locationNotValid.Visibility = System.Windows.Visibility.Hidden;
            this.loginNotValid.Visibility = System.Windows.Visibility.Hidden;
            this.passwordNotValid.Visibility = System.Windows.Visibility.Hidden;
            this.confirmNotValid.Visibility = System.Windows.Visibility.Hidden;

        }

        private void IsImportChecked(object sender, RoutedEventArgs e)
        {
            this.ShowImportControls();
            this.CreateCB.IsChecked = false;
        }

        private void IsCreateChecked(object sender, RoutedEventArgs e)
        {
            this.HideImportControls();
            this.ImportCB.IsChecked = false;
        }

        private void HideImportControls()
        {
            this.DBLoginInput.IsEnabled = false;
            this.DBNameInput.IsEnabled = false;
            this.DBPasswordInput.IsEnabled = false;
            this.DBConfirmInput.IsEnabled = false;

            this.DBLoginLabel.Foreground = new SolidColorBrush(Colors.Gray);
            this.DBNameLabel.Foreground = new SolidColorBrush(Colors.Gray);
            this.DBPasswordLabel.Foreground = new SolidColorBrush(Colors.Gray);
            this.DBConfirmLabel.Foreground = new SolidColorBrush(Colors.Gray);
        }

        private void ShowImportControls()
        {
            this.DBLoginInput.IsEnabled = true;
            this.DBNameInput.IsEnabled = true;
            this.DBPasswordInput.IsEnabled = true;
            this.DBConfirmInput.IsEnabled = true;

            this.DBLoginLabel.Foreground = new SolidColorBrush(Colors.Black);
            this.DBNameLabel.Foreground = new SolidColorBrush(Colors.Black);
            this.DBPasswordLabel.Foreground = new SolidColorBrush(Colors.Black);
            this.DBConfirmLabel.Foreground = new SolidColorBrush(Colors.Black);
        }

        private void GoBackToStep1(object sender, RoutedEventArgs e)
        {
            MessageBoxResult MBResult = MessageBox.Show("Etes-vous sûr de vouloir revenir à l'étape précédente ?",
                                                        "Validation",
                                                         MessageBoxButton.YesNo);

            if (MBResult == MessageBoxResult.Yes)
            {
                Step1Window step1 = new Step1Window();
                step1.Show();
                this.Close();
            }
        }

        private void GoToNextStep(object sender, RoutedEventArgs e)
        {
            MessageBoxResult MBResult;

            if ((bool)this.ImportCB.IsChecked)
            {
                if (this.DBNameInput.Text == "")
                    this.locationNotValid.Visibility = System.Windows.Visibility.Visible;
                else
                    this.locationNotValid.Visibility = System.Windows.Visibility.Hidden;

                if (this.DBLoginInput.Text == "")
                    this.loginNotValid.Visibility = System.Windows.Visibility.Visible;
                else
                    this.loginNotValid.Visibility = System.Windows.Visibility.Hidden;

                if (this.DBPasswordInput.Password == "")
                    this.passwordNotValid.Visibility = System.Windows.Visibility.Visible;
                else
                {
                    this.passwordNotValid.Visibility = System.Windows.Visibility.Hidden;

                    if (this.DBPasswordInput.Password.Equals(this.DBConfirmInput.Password))
                        this.confirmNotValid.Visibility = System.Windows.Visibility.Hidden;
                    else
                        this.confirmNotValid.Visibility = System.Windows.Visibility.Visible;
                }
                



                if (this.DBNameInput.Text != "" && this.DBLoginInput.Text != "" && this.DBPasswordInput.Password != "" && (this.DBPasswordInput.Password.Equals(this.DBConfirmInput.Password)))
                {
                

                    MBResult = MessageBox.Show("Vous avez choisi la location "
                                                                + this.DBNameInput.Text
                                                                + " et le login "
                                                                + this.DBLoginInput.Text
                                                                + ".\nEtes-vous sur de votre choix ?"
                                                                , "Validation"
                                                                , MessageBoxButton.YesNo);

                    if (MBResult == MessageBoxResult.Yes)
                    {
                            CurrentProjectSingleton.getInstance.ConnectionString.DataSource = this.DBNameInput.Text;
                            CurrentProjectSingleton.getInstance.ConnectionString.Login = this.DBLoginInput.Text;
                            CurrentProjectSingleton.getInstance.ConnectionString.Password = this.DBPasswordInput.Password;

                            MessageBox.Show(CurrentProjectSingleton.getInstance.toString());
                            //Go to next Step
                    }
                }
            }
            else
            {
                 if ((bool)this.CreateCB.IsChecked)
                 {
                     MBResult = MessageBox.Show("Vous avez choisi la création d'une nouvelle base de données."
                                                   + "\nVous allez être redirigé vers l'assistant de création BOGenerator, merci de confirmer votre choix"
                                                   , "Confirmation"
                                                   , MessageBoxButton.YesNo);

                     if (MBResult == MessageBoxResult.Yes)
                         MessageBox.Show(CurrentProjectSingleton.getInstance.toString());
                         //Call DB creator program (cf. Romain)
                 }
                 else
                     MBResult = MessageBox.Show("Merci de choisir entre l'import ou la création de base de données.");
             }  
        }      
    }
}
